package com.ecommerce.service;

import com.ecommerce.dao.CouponDAO;
import com.ecommerce.model.Coupon;
import com.ecommerce.util.InputUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CouponService {
    private CouponDAO couponDAO;

    public CouponService() {
        this.couponDAO = new CouponDAO();
    }

    public void createCoupon() {
        System.out.println("\n=== 创建优惠券 ===");

        String code = InputUtil.getStringInput("优惠码: ");
        String description = InputUtil.getStringInput("优惠券描述: ");
        double discountAmount = InputUtil.getDoubleInput("折扣金额: ");
        double minOrderAmount = InputUtil.getDoubleInput("最低消费金额: ");
        int usageLimit = InputUtil.getIntInput("使用次数限制: ");

        try {
            String startInput = InputUtil.getStringInput("开始日期 (yyyy-MM-dd): ");
            String endInput = InputUtil.getStringInput("结束日期 (yyyy-MM-dd): ");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = sdf.parse(startInput);
            Date endDate = sdf.parse(endInput);

            // 验证结束日期是否晚于开始日期
            if (endDate.before(startDate)) {
                System.out.println("结束日期不能早于开始日期！");
                return;
            }

            Coupon coupon = new Coupon(code, description, discountAmount, minOrderAmount, startDate, endDate, usageLimit);
            boolean success = couponDAO.createCoupon(coupon);

            if (success) {
                System.out.println("优惠券创建成功！");
            } else {
                System.out.println("优惠券创建失败！");
            }

        } catch (Exception e) {
            System.out.println("日期格式错误！请使用 yyyy-MM-dd 格式");
        }
    }

    public void displayAllCoupons() {
        List<Coupon> coupons = couponDAO.getAllCoupons();

        System.out.println("\n=== 所有优惠券 ===");
        if (coupons.isEmpty()) {
            System.out.println("暂无优惠券");
        } else {
            for (Coupon coupon : coupons) {
                System.out.println("ID: " + coupon.getId() + " | " + coupon);
            }
        }
        System.out.println("总计: " + coupons.size() + " 张优惠券");
        System.out.println("=================");
    }

    public void viewCouponDetails() {
        System.out.println("\n=== 查看优惠券详情 ===");
        displayAllCoupons();

        int couponId = InputUtil.getIntInput("请输入优惠券ID: ");
        Coupon coupon = couponDAO.getCouponById(couponId);

        if (coupon != null) {
            System.out.println("\n=== 优惠券详情 ===");
            System.out.println(coupon.toDetailedString());
        } else {
            System.out.println("优惠券不存在！");
        }
    }

    public void updateCoupon() {
        System.out.println("\n=== 更新优惠券 ===");
        displayAllCoupons();

        int couponId = InputUtil.getIntInput("请输入要更新的优惠券ID: ");
        Coupon coupon = couponDAO.getCouponById(couponId);

        if (coupon == null) {
            System.out.println("优惠券不存在！");
            return;
        }

        System.out.println("当前优惠券信息:");
        System.out.println(coupon.toDetailedString());

        String code = InputUtil.getStringInput("新优惠码 (留空保持不变): ");
        String description = InputUtil.getStringInput("新描述 (留空保持不变): ");
        String discountInput = InputUtil.getStringInput("新折扣金额 (留空保持不变): ");
        String minOrderInput = InputUtil.getStringInput("新最低消费金额 (留空保持不变): ");
        String usageLimitInput = InputUtil.getStringInput("新使用次数限制 (留空保持不变): ");

        // 更新字段
        if (!code.isEmpty()) coupon.setCode(code);
        if (!description.isEmpty()) coupon.setDescription(description);
        if (!discountInput.isEmpty()) coupon.setDiscountAmount(Double.parseDouble(discountInput));
        if (!minOrderInput.isEmpty()) coupon.setMinOrderAmount(Double.parseDouble(minOrderInput));
        if (!usageLimitInput.isEmpty()) coupon.setUsageLimit(Integer.parseInt(usageLimitInput));

        boolean success = couponDAO.updateCoupon(coupon);
        if (success) {
            System.out.println("优惠券更新成功！");
        } else {
            System.out.println("优惠券更新失败！");
        }
    }

    public void toggleCouponStatus() {
        System.out.println("\n=== 启用/禁用优惠券 ===");
        displayAllCoupons();

        int couponId = InputUtil.getIntInput("请输入优惠券ID: ");
        Coupon coupon = couponDAO.getCouponById(couponId);

        if (coupon == null) {
            System.out.println("优惠券不存在！");
            return;
        }

        String currentStatus = "active".equals(coupon.getStatus()) ? "启用" : "禁用";
        String newStatus = "active".equals(coupon.getStatus()) ? "禁用" : "启用";

        System.out.println("当前状态: " + currentStatus);
        String confirm = InputUtil.getStringInput("确认" + newStatus + "此优惠券？(y/N): ");

        if ("y".equalsIgnoreCase(confirm)) {
            boolean success = couponDAO.toggleCouponStatus(couponId);
            if (success) {
                System.out.println("优惠券" + newStatus + "成功！");
            } else {
                System.out.println("操作失败！");
            }
        } else {
            System.out.println("取消操作");
        }
    }

    public void deleteCoupon() {
        System.out.println("\n=== 删除优惠券 ===");
        displayAllCoupons();

        int couponId = InputUtil.getIntInput("请输入要删除的优惠券ID: ");
        Coupon coupon = couponDAO.getCouponById(couponId);

        if (coupon == null) {
            System.out.println("优惠券不存在！");
            return;
        }

        System.out.println("要删除的优惠券信息:");
        System.out.println(coupon.toDetailedString());

        String confirm = InputUtil.getStringInput("确认删除？请输入 'DELETE' 确认: ");
        if (!"DELETE".equals(confirm)) {
            System.out.println("取消删除");
            return;
        }

        boolean success = couponDAO.deleteCoupon(couponId);
        if (success) {
            System.out.println("优惠券删除成功！");
        } else {
            System.out.println("优惠券删除失败！");
        }
    }

    public void manageCoupons() {
        while (true) {
            System.out.println("\n=== 优惠券管理 ===");
            System.out.println("1. 查看所有优惠券");
            System.out.println("2. 创建优惠券");
            System.out.println("3. 查看优惠券详情");
            System.out.println("4. 更新优惠券");
            System.out.println("5. 启用/禁用优惠券");
            System.out.println("6. 删除优惠券");
            System.out.println("7. 返回上级菜单");

            int choice = InputUtil.getIntInput("请选择操作: ");

            switch (choice) {
                case 1:
                    displayAllCoupons();
                    break;
                case 2:
                    createCoupon();
                    break;
                case 3:
                    viewCouponDetails();
                    break;
                case 4:
                    updateCoupon();
                    break;
                case 5:
                    toggleCouponStatus();
                    break;
                case 6:
                    deleteCoupon();
                    break;
                case 7:
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    // 提供给订单服务使用的方法
    public Coupon validateCoupon(String code, double orderAmount) {
        Coupon coupon = couponDAO.getCouponByCode(code);
        if (coupon != null && coupon.canApply(orderAmount)) {
            return coupon;
        }
        return null;
    }

    public boolean useCoupon(int couponId) {
        return couponDAO.useCoupon(couponId);
    }
}